<template>
    <el-row>
        <el-card shadow="never" style="margin-bottom:24px">
            <el-row style="margin-bottom:24px">
                <el-col :span="12">商品概况</el-col>
                <el-col :span="12" style="text-align:right">
                    <el-date-picker v-model="date" type="date" placeholder="选择日期" size="small" style="width:240px" @change="changeTime">
                    </el-date-picker>
                </el-col>
            </el-row>
            <el-row style="padding-bottom:12px;border-bottom:1px solid #eee">
                <el-col :span="3">
                    <el-card shadow="never" class="left-col">商品</el-card>
                </el-col>
                <el-col :span="5" class="right-col">
                    <el-row>在售商品数</el-row>
                    <el-row class="right-col-num">{{productCount}}</el-row>
                </el-col>
            </el-row>
            <el-row style="padding:12px 0;border-bottom:1px solid #eee">
                <el-col :span="3">
                    <el-card shadow="never" class="left-col">访问</el-card>
                </el-col>
                <el-col :span="5" class="right-col">
                    <el-row>被访问商品数</el-row>
                    <el-row class="right-col-num">{{productSum.visit}}</el-row>
                </el-col>
                <el-col :span="5" class="right-col">
                    <el-row>商品访客数</el-row>
                    <el-row class="right-col-num">{{productSum.visitor}}</el-row>
                </el-col>
                <el-col :span="5" class="right-col">
                    <el-row>商品浏览量</el-row>
                    <el-row class="right-col-num">{{productSum.view}}</el-row>
                </el-col>
            </el-row>
            <el-row style="padding-top:12px">
                <el-col :span="3">
                    <el-card shadow="never" class="left-col">转化</el-card>
                </el-col>
                <el-col :span="5" class="right-col">
                    <el-row>商品转化率</el-row>
                    <el-row class="right-col-num">{{percent}}%</el-row>
                </el-col>
            </el-row>
        </el-card>
        <el-card shadow="never">
            <el-row style="margin-bottom:24px">
                <el-col :span="12">商品效果</el-col>
                <el-col :span="12" style="text-align:right">
                    <el-date-picker v-model="dateList" type="date" placeholder="选择日期" size="small" style="width:240px" @change="changeTimeList">
                    </el-date-picker>
                </el-col>
            </el-row>
            <el-table :data="data">
                <el-table-column label="商品信息" header-align="center">
                    <template slot-scope="scope">
                        <div style="display:flex">
                            <img :src="scope.row.product.img_url" alt="" style="width:60px;height:60px">
                            <div style="flex:1;padding:2px 7px">
                                <div class="text_overflow text_overflow_line_2">{{scope.row.product.name}}</div>
                                <div style="color:#f50;margin-top:8px">¥ {{scope.row.product.price}}</div>
                            </div>
                        </div>
                    </template>
                </el-table-column>
                <el-table-column prop="visitor" label="访客数" header-align="center" align="center"></el-table-column>
                <el-table-column prop="view" label="浏览量" header-align="center" align="center"></el-table-column>
                <el-table-column prop="trade" label="付款人数" header-align="center" align="center"></el-table-column>
                <el-table-column label="单品转化率" header-align="center" align="center">
                    <template slot-scope="scope">
                        {{((scope.row.trade/scope.row.visitor)*100).toFixed(2)}}%
                    </template>
                </el-table-column>
            </el-table>
            <el-pagination layout="total,prev,pager,next" background :total="pagination.rowCount" :current-page="pagination.page" :page-size="pagination.pageSize" @current-change="changePage" style="float:right;margin:24px 0"></el-pagination>
        </el-card>
    </el-row>
</template>

<script>
export default {
    layout: "shop",
    data() {
        return {
            date: new Date(),
            productCount: 0, //在售商品数
            productSum: {
                visit: 0, //被访问商品数
                visitor: 0, //商品访客数
                view: 0 //商品浏览数
            },
            percent:0,//转化率
            dateList:new Date(),
            data: [],
            pagination:{
                page:1
            }
        };
    },
    mounted() {
        this.getData();
        this.getDataList();
    },
    methods: {
        async getData() {
            //商品分析
            const res = await this.$axios.$get(`/api/app/home/analysis/product?date=${this.date}`);
            if (res && res.errmsg === "ok") {
                this.productCount = res.data.productCount;
                if (res.data.productSum) {
                    this.productSum = res.data.productSum;
                    this.percent = res.data.percent;
                } else {
                    this.productSum = {
                        visit: 0, 
                        visitor: 0, 
                        view: 0 
                    };
                    this.percent = 0;
                }
            }
        },
        changeTime(value) {
            if (value) {
                this.date = value;
                this.getData();
            }
        },
        async getDataList() {
            //商品分析列表
            const res = await this.$axios.$get(`/api/app/home/analysis/productList?date=${this.dateList}&page=${this.pagination.page}`);
            if (res && res.errmsg === "ok") {
                this.data = res.data.data;
                this.pagination = res.data.pagination;
            }
        },
        changePage(page){
            this.pagination.page = page;
            this.getDataList();
        },
        changeTimeList(value) {
            if (value) {
                this.dateList = value;
                this.pagination.page = 1;
                this.getDataList();
            }
        }
    }
};
</script>

<style scoped>
.left-col {
    height: 100px;
    line-height: 60px;
    text-align: center;
}

.right-col {
    text-align: center;
    padding: 22px 0;
}

.right-col-num {
    font-size: 20px;
    color: #000;
    font-weight: 500;
    margin-top: 6px;
}
</style>
